package com.carbohydrate42.kotlin_compose_demo.compose

import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp


/**
 * 参考
 *
 * 获取 activity 的返回内容
 * https://developer.android.com/training/data-storage/shared/photopicker?hl=zh-cn
 * jetpack compose 获取 activity 的返回内容
 * https://developer.android.google.cn/jetpack/compose/libraries?hl=zh-cn
 */
@OptIn(ExperimentalMaterial3Api::class)
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun FilePickerAndPreviewer() {
    val context = LocalContext.current

    var applicationCacheDirFileList by remember {
        mutableStateOf<List<String>>(emptyList())
    }

    var applicationFilesDirFileList by remember {
        mutableStateOf<List<String>>(emptyList())
    }

    var applicationExternalFilesList by remember {
        mutableStateOf<List<String>>(emptyList())
    }

    var applicationExternalCacheFileList by remember {
        mutableStateOf<List<String>>(emptyList())
    }


    // activity-ktx 给 compose 附带了一个 hook : rememberLauncherForActivityResult, 简化了获取 registerForActivityResult 返回内容的流程
    var resultMediaUri by remember { mutableStateOf<Uri?>(null) }

    val pickMedia =
        rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
            resultMediaUri = uri
        }

    var resultMediaUriList by remember { mutableStateOf<List<Uri>?>(emptyList()) }

    val pickMultipleMedia =
        rememberLauncherForActivityResult(ActivityResultContracts.PickMultipleVisualMedia()) {
            resultMediaUriList = it
        }


    var resultDocumentUriList by remember { mutableStateOf<List<Uri>?>(emptyList()) }

    val openMultipleDocuments =
        rememberLauncherForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) {
            resultDocumentUriList = it
        }

    Column(
        modifier = Modifier
            .verticalScroll(rememberScrollState())
            .padding(16.dp)
            .fillMaxSize()
    ) {

        Button(onClick = {
            pickMedia.launch(
                PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo)
            )
        }) {
            Text(text = "从系统相册单选照片或视频")
        }

        Text(text = "单选的照片或视频路径: ${resultMediaUri?.path}")

        Divider()

        Button(onClick = {
            pickMultipleMedia.launch(
                PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo)
            )
        }) {
            Text(text = "从系统相册多选照片或视频 (总之默认返回的是一个URI的数组)")
        }

        Column {
            Text(text = "多选的照片或视频路径: ")

            resultMediaUriList?.forEach {
                Text(text = it.path.toString())
            }
        }

        Divider()

        Button(onClick = {
            openMultipleDocuments.launch(emptyArray())
        }) {
            Text(text = "从系统文件目录多选文件 (总之默认返回的是一个URI的数组)")
        }

        Column {
            Text(text = "多选文件路径: ")

            resultDocumentUriList?.forEach {
                Text(text = it.path.toString())
            }
        }


        Divider()


        Button(onClick = {
            applicationCacheDirFileList = context.cacheDir?.list()?.toList() ?: emptyList()

            applicationFilesDirFileList = context.fileList().toList()

            val externalCacheFiles = ArrayList<String>()
            context.externalCacheDirs.toList().forEach { dir ->
                dir.walk().forEach { fileOrDir ->
                    externalCacheFiles.add(fileOrDir.path)
                }
            }

            applicationExternalCacheFileList = externalCacheFiles.toList()

            val externalFiles = ArrayList<String>()
            context.getExternalFilesDirs(null).toList().forEach { dir ->
                dir.walk().forEach { fileOrDir ->
                    externalFiles.add(fileOrDir.path)
                }
            }
            applicationExternalFilesList = externalFiles.toList()
        }) {
            Text(text = "获取 app 缓存目录下的文件列表")
        }


        Text(text = "applicationCacheDirFileList : ", color = Color.Red)
        applicationCacheDirFileList.forEach {
            Text(text = it)
        }

        Text(text = "applicationFilesDirFileList : ", color = Color.Red)
        applicationFilesDirFileList.forEach {
            Text(text = it)
        }

        Text(text = "applicationExternalCacheFileList : ", color = Color.Red)
        applicationExternalCacheFileList.forEach {
            Text(text = it)
        }

        Text(text = "applicationExternalCacheFileList : ", color = Color.Red)
        applicationExternalFilesList.forEach {
            Text(text = it)
        }
    }

}

